<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div style="max-width: 1000px">
        <a-table :loading="loading" rowKey="id" :pagination="false" :columns="columns" :data-source="checkList" :scroll="{y: 600}">
          <div slot="cover_pic" slot-scope="value">
            <img style="width: 40px; height: 40px" :src="value" alt="" />
          </div>
          <div slot="action" slot-scope="item">
            <a-button type="link" @click="onDel(item)"> 删除 </a-button>
          </div>
        </a-table>
      </div>

      <div style="margin-top: 20px">
        <a-button type="primary" @click="onShowPopup"> 选择商品 </a-button>
        <a-button :loading="confirmLoading" style="margin-left: 20px" type="success" @click="onSubmit">
          提交保存
        </a-button>
      </div>
    </a-card>

    <PopGoods :checkList="checkList" @onOk="selectGoods" ref="popGoods"></PopGoods>
  </page-header-wrapper>
</template>

<script>
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '商品主图',
    dataIndex: 'cover_pic',
    scopedSlots: { customRender: 'cover_pic' },
  },
  {
    title: '商品名称',
    dataIndex: 'name',
    ellipsis: true,
    width: '30%',
  },
  {
    title: '商品库存',
    dataIndex: 'goods_num',
  },
  {
    title: '操作',
    scopedSlots: { customRender: 'action' },
  },
]

import PopGoods from '@/views/pt/edit/components/goods.vue'
import { miniAuditGoods, miniAuditGoodsSave } from '@/api/setting/setting'
export default {
  components: {
    PopGoods,
  },
  data() {
    return {
      columns,
      showGoodsPopup: false,
      checkList: [],
      loading: false,
      confirmLoading: false,
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList(reset) {
      this.loading = true
      const res = await miniAuditGoods({})
      this.checkList = res.goods_list
      this.loading = false
    },
    selectGoods(list) {
      this.checkList = JSON.parse(JSON.stringify(list))
    },
    onShowPopup() {
      this.$refs['popGoods'].visible = true
    },
    onDel(it) {
      this.checkList = this.checkList.filter((item) => item.id != it.id)
    },
    async onSubmit() {
      this.confirmLoading = true
      var ids = ''
      this.checkList.forEach((item) => {
        ids += `${item.id},`
      })
      console.log(ids)
      ids = ids.substring(0, ids.length - 1)
      console.log(ids)
      const res = await miniAuditGoodsSave({
        mini_audit_goods_ids: ids,
      })
      this.$message.success('保存成功')
      this.confirmLoading = false
    },
  },
}
</script>

<style lang="scss" scoped></style>
